package com.shenmazong.serveraopdemo02.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @program: server-java-demo
 * @description: LogAspect
 * @author: 亮子说编程
 * @create: 2020-12-02 09:12
 **/

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Before(value = "execution(* com.shenmazong.serveraopdemo02.controller.*.*(..))")
    public void beforeMethod(JoinPoint jp) {
        String methodName = jp.getSignature().getName();
        //new ObjectMapper().writeValueAsString(jp.getArgs())
        log.info("【前置增强】the method 【" + methodName + "】 begins with " + jp.getArgs().toString());
    }

    @After(value = "execution(* com.shenmazong.serveraopdemo02.controller.*.*(..))")
    public void afterMethod(JoinPoint jp) {
        log.info("【后置增强】this is a afterMethod advice...");
    }

    @AfterReturning(value = "execution(* com.shenmazong.serveraopdemo02.controller.*.*(..))", returning = "result")
    public void afterReturn(JoinPoint jp, Object result) {
        String methodName = jp.getSignature().getName();
        log.info("【返回增强】the method 【" + methodName + "】 ends with 【" + result + "】");
    }

    @AfterThrowing(value = "execution(* com.shenmazong.serveraopdemo02.controller.*.*(..))", throwing = "e")
    public void afterThrowing(JoinPoint jp, Exception e) {
        String methodName = jp.getSignature().getName();
        log.error("【异常增强】the method 【" + methodName + "】 occurs exception: ", e);
    }
}
